// 引入express
const express = require("express");
const { request, response } = require("express");

// 创建应用对象
const app = express();

// 创建路由规则
// request请求报文封装 response响应报文封装
app.get("/delay", (request, response) => {
    // 设置响应头 设置允许跨域
    response.setHeader("Access-Control-Allow-Origin", "*")

    // 设置响应
    let data = {
        name: "kaka",
        number: 222
    }
    setTimeout(() => {
        response.send(JSON.stringify(data));
    },3000);
});

app.post("/server", (request, response) => {
    // 设置响应头 设置允许跨域
    response.setHeader("Access-Control-Allow-Origin", "*")

    // 设置响应
    response.send("Hello AJAX ---post!")
});

app.all("/axios-server", (request, response) => {
    response.setHeader("Access-Control-Allow-Origin", "http://127.0.0.1:5500");
    response.setHeader("Access-Control-Allow-Headers", "*")
    let data = {
        name: "kaka",
        age: "40"
    }
    response.send(JSON.stringify(data));
});

app.all("/fetch-server", (request, response) => {
    response.setHeader("Access-Control-Allow-Origin", "*")
    response.setHeader("Access-Control-Allow-Headers", "*")
    let data = {
        name: "kaka",
        age: "40"
    }
    response.send(JSON.stringify(data));
});

// 监听端口
app.listen(8888, () => {
    console.log("服务已启动，8888监听端口...");
});